{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 机器学习-K近邻\n",
    "\n",
    "### Airbnb 房价预测任务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"1.png\" style=\"width:800px;height:480px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3723, 8)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accommodates</th>\n",
       "      <th>bedrooms</th>\n",
       "      <th>bathrooms</th>\n",
       "      <th>beds</th>\n",
       "      <th>price</th>\n",
       "      <th>minimum_nights</th>\n",
       "      <th>maximum_nights</th>\n",
       "      <th>number_of_reviews</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>$160.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>$350.00</td>\n",
       "      <td>2</td>\n",
       "      <td>30</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>$50.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>$95.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>$50.00</td>\n",
       "      <td>7</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   accommodates  bedrooms  bathrooms  beds    price  minimum_nights  \\\n",
       "0             4       1.0        1.0   2.0  $160.00               1   \n",
       "1             6       3.0        3.0   3.0  $350.00               2   \n",
       "2             1       1.0        2.0   1.0   $50.00               2   \n",
       "3             2       1.0        1.0   1.0   $95.00               1   \n",
       "4             4       1.0        1.0   1.0   $50.00               7   \n",
       "\n",
       "   maximum_nights  number_of_reviews  \n",
       "0            1125                  0  \n",
       "1              30                 65  \n",
       "2            1125                  1  \n",
       "3            1125                  0  \n",
       "4            1125                  0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "features = ['accommodates','bedrooms','bathrooms','beds','price','minimum_nights','maximum_nights','number_of_reviews']\n",
    "\n",
    "dc_listings = pd.read_csv('listings.csv')\n",
    "\n",
    "dc_listings = dc_listings[features]\n",
    "print(dc_listings.shape)\n",
    "\n",
    "dc_listings.head()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据特征：\n",
    "\n",
    "* accommodates: 可以容纳的旅客\n",
    "* bedrooms: 卧室的数量\n",
    "* bathrooms: 厕所的数量\n",
    "* beds: 床的数量\n",
    "* price: 每晚的费用\n",
    "* minimum_nights: 客人最少租了几天\n",
    "* maximum_nights: 客人最多租了几天\n",
    "* number_of_reviews: 评论的数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如果我有一个3个房间的房子，我能租多少钱呢？\n",
    "讲道理，咱是不是得去看看3个房间的别人都租到多少钱啊！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"2.png\" style=\"width:600px;height:230px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "K代表我们的候选对象个数，也就是找和我房间数量最相近的其他房子的价格"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## K近邻原理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"3.png\" style=\"width:600px;height:330px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假设我们的数据源中只有5条信息，现在我想针对我的房子（只有一个房间）来定一个价格。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"4.png\" style=\"width:600px;height:330px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这里假设我们选择的K=3，也就是选3个跟我最相近的房源。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"5.png\" style=\"width:600px;height:330px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "再综合考虑这三个我就得到了我的房子大概能值多钱啦！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 距离的定义\n",
    "如何才能知道哪些数据样本跟我最相近呢？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"6.png\" style=\"width:400px;height:80px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其中Qi到Qn是一条数据的所有特征信息，P1到Pn是另一条数据的所有特征信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假设我们的房子有3个房间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      461\n",
       "1     2294\n",
       "2      503\n",
       "3      279\n",
       "4       35\n",
       "5       73\n",
       "6       17\n",
       "7       22\n",
       "8        7\n",
       "9       12\n",
       "10       2\n",
       "11       4\n",
       "12       6\n",
       "13       8\n",
       "Name: distance, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "our_acc_value = 3\n",
    "\n",
    "dc_listings['distance'] = np.abs(dc_listings.accommodates - our_acc_value)\n",
    "dc_listings.distance.value_counts().sort_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里我们只有了绝对值来计算，和我们距离为0的（同样数量的房间）有461个"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sample操作可以得到洗牌后的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3723, 9)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2645     $75.00\n",
       "2825    $120.00\n",
       "2145     $90.00\n",
       "2541     $50.00\n",
       "3349    $105.00\n",
       "Name: price, dtype: object"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#help(dc_listings.sample)\n",
    "dc_listings = dc_listings.sample(frac=1,random_state=0)\n",
    "print(dc_listings.shape)\n",
    "dc_listings = dc_listings.sort_values('distance')\n",
    "dc_listings.price.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在的问题是，这里面的数据是字符串呀，需要转换一下！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "Can only use .str accessor with string values, which use np.object_ dtype in pandas",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-773f786dc56d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdc_listings\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'price'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdc_listings\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprice\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\\$|,\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mmean_price\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdc_listings\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprice\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mmean_price\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\sorfware_install\\python_install\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m   3075\u001b[0m         if (name in self._internal_names_set or name in self._metadata or\n\u001b[0;32m   3076\u001b[0m                 name in self._accessors):\n\u001b[1;32m-> 3077\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mobject\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__getattribute__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   3078\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3079\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mname\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_info_axis\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\sorfware_install\\python_install\\lib\\site-packages\\pandas\\core\\base.py\u001b[0m in \u001b[0;36m__get__\u001b[1;34m(self, instance, owner)\u001b[0m\n\u001b[0;32m    241\u001b[0m             \u001b[1;31m# this ensures that Series.str.<method> is well defined\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    242\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maccessor_cls\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 243\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconstruct_accessor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minstance\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    244\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    245\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__set__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minstance\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\sorfware_install\\python_install\\lib\\site-packages\\pandas\\core\\strings.py\u001b[0m in \u001b[0;36m_make_str_accessor\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   1907\u001b[0m             \u001b[1;31m# (instead of test for object dtype), but that isn't practical for\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1908\u001b[0m             \u001b[1;31m# performance reasons until we have a str dtype (GH 9343)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1909\u001b[1;33m             raise AttributeError(\"Can only use .str accessor with string \"\n\u001b[0m\u001b[0;32m   1910\u001b[0m                                  \u001b[1;34m\"values, which use np.object_ dtype in \"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1911\u001b[0m                                  \"pandas\")\n",
      "\u001b[1;31mAttributeError\u001b[0m: Can only use .str accessor with string values, which use np.object_ dtype in pandas"
     ]
    }
   ],
   "source": [
    "dc_listings['price'] = dc_listings.price.str.replace(\"\\$|,\",'').astype(float)\n",
    "\n",
    "#mean_price = dc_listings.price.iloc[:5].mean()\n",
    "#mean_price"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "得到了平均价格，也就是我们的房子大致的价格了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型的评估"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"7.png\" style=\"width:600px;height:250px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先制定好训练集和测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dc_listings.drop('distance',axis=1)\n",
    "\n",
    "train_df = dc_listings.copy().iloc[:2792]\n",
    "test_df = dc_listings.copy().iloc[2792:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基于单变量预测价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predict_price(new_listing_value,feature_column):\n",
    "    temp_df = train_df\n",
    "    temp_df['distance'] = np.abs(dc_listings[feature_column] - new_listing_value)\n",
    "    temp_df = temp_df.sort_values('distance')\n",
    "    knn_5 = temp_df.price.iloc[:5]\n",
    "    predicted_price = knn_5.mean()\n",
    "    return(predicted_price)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accommodates</th>\n",
       "      <th>bedrooms</th>\n",
       "      <th>bathrooms</th>\n",
       "      <th>beds</th>\n",
       "      <th>price</th>\n",
       "      <th>minimum_nights</th>\n",
       "      <th>maximum_nights</th>\n",
       "      <th>number_of_reviews</th>\n",
       "      <th>distance</th>\n",
       "      <th>predicted_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2850</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>105</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2279</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2771</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>3</td>\n",
       "      <td>730</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>910</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>415.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2434</th>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>275.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>965</th>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>145.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1000</td>\n",
       "      <td>74</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1305</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>21</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2513</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2118</th>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>115.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>43</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>345</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>324.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>725</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1172</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>116.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1409</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>90.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1943</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2842</th>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>136.0</td>\n",
       "      <td>7</td>\n",
       "      <td>14</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2967</th>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>125.0</td>\n",
       "      <td>2</td>\n",
       "      <td>365</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3295</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2000</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3558</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>90.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1698</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>165.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1125</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3151</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7</td>\n",
       "      <td>14</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>179.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>195</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>754</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1767</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>120.0</td>\n",
       "      <td>1</td>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1241</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>75.0</td>\n",
       "      <td>14</td>\n",
       "      <td>180</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1149</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>11</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1931</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3535</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>165.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3422</th>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>250.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2969</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>83.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>542</th>\n",
       "      <td>12</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>4.0</td>\n",
       "      <td>340.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>529</th>\n",
       "      <td>12</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>600.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1878</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>1</td>\n",
       "      <td>365</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2995</th>\n",
       "      <td>12</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>595.0</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3272</th>\n",
       "      <td>12</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>7.0</td>\n",
       "      <td>599.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3305</th>\n",
       "      <td>12</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1326</th>\n",
       "      <td>12</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.5</td>\n",
       "      <td>4.0</td>\n",
       "      <td>375.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1656</th>\n",
       "      <td>12</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>6.0</td>\n",
       "      <td>283.0</td>\n",
       "      <td>4</td>\n",
       "      <td>90</td>\n",
       "      <td>26</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1875</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>1</td>\n",
       "      <td>365</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>808</th>\n",
       "      <td>12</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>215.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1825</td>\n",
       "      <td>34</td>\n",
       "      <td>9</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>571</th>\n",
       "      <td>13</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3524</th>\n",
       "      <td>13</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>295.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>685</th>\n",
       "      <td>14</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>399.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1403</th>\n",
       "      <td>14</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>599.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>562</th>\n",
       "      <td>14</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>599.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1658</th>\n",
       "      <td>14</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>7.0</td>\n",
       "      <td>283.0</td>\n",
       "      <td>4</td>\n",
       "      <td>90</td>\n",
       "      <td>19</td>\n",
       "      <td>11</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>657</th>\n",
       "      <td>15</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1</td>\n",
       "      <td>28</td>\n",
       "      <td>41</td>\n",
       "      <td>12</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2688</th>\n",
       "      <td>15</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>10.0</td>\n",
       "      <td>749.0</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1850</th>\n",
       "      <td>15</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>180.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>26</td>\n",
       "      <td>12</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1806</th>\n",
       "      <td>15</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>330.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>6</td>\n",
       "      <td>12</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2106</th>\n",
       "      <td>15</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>12.0</td>\n",
       "      <td>499.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2757</th>\n",
       "      <td>15</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>245.0</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>611</th>\n",
       "      <td>16</td>\n",
       "      <td>8.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1250.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1818</th>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>16.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1402</th>\n",
       "      <td>16</td>\n",
       "      <td>8.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1200.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>10</td>\n",
       "      <td>13</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>763</th>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2560</th>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>3</td>\n",
       "      <td>60</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1594</th>\n",
       "      <td>16</td>\n",
       "      <td>10.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1250.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1224</th>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>499.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1125</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1596</th>\n",
       "      <td>16</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>3</td>\n",
       "      <td>365</td>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "      <td>340.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>931 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      accommodates  bedrooms  bathrooms  beds   price  minimum_nights  \\\n",
       "2850             1       1.0        1.0   1.0    40.0               2   \n",
       "2279             1       1.0        1.0   1.0    45.0               1   \n",
       "2771             5       2.0        2.0   3.0   217.0               3   \n",
       "910              5       2.0        2.0   3.0   415.0               1   \n",
       "2434             5       1.0        1.0   3.0   275.0               3   \n",
       "965              5       1.0        1.0   2.0   145.0               4   \n",
       "1305             1       1.0        1.0   1.0   100.0               2   \n",
       "2513             1       1.0        1.5   1.0    80.0               2   \n",
       "2118             5       1.0        1.0   1.0   115.0               1   \n",
       "345              5       2.0        2.0   3.0   324.0               3   \n",
       "725              1       1.0        1.0   1.0    50.0               1   \n",
       "1172             1       0.0        1.0   1.0   116.0               1   \n",
       "1409             1       1.0        1.5   1.0    90.0               2   \n",
       "1943             1       1.0        1.0   1.0    49.0               1   \n",
       "2842             5       1.0        2.0   1.0   136.0               7   \n",
       "2967             5       1.0        1.0   2.0   125.0               2   \n",
       "3295             1       1.0        1.0   1.0    50.0               1   \n",
       "3558             1       1.0        1.0   1.0    90.0               1   \n",
       "1698             5       2.0        2.5   2.0   165.0               4   \n",
       "3151             1       1.0        1.0   1.0    95.0               7   \n",
       "2025             5       2.0        1.0   3.0   179.0               3   \n",
       "754              1       1.0        1.0   3.0    38.0               2   \n",
       "252              1       1.0        1.0   1.0   135.0               1   \n",
       "1767             1       1.0        1.0   1.0   120.0               1   \n",
       "1241             1       1.0        1.0   1.0    75.0              14   \n",
       "1149             1       1.0        2.0   1.0   100.0               2   \n",
       "1931             1       1.0        2.0   1.0    50.0               1   \n",
       "3535             1       1.0        1.0   1.0   165.0               1   \n",
       "3422             5       2.0        1.0   3.0   250.0               1   \n",
       "2969             1       1.0        1.0   1.0   100.0               1   \n",
       "...            ...       ...        ...   ...     ...             ...   \n",
       "542             12       4.0        3.5   4.0   340.0               2   \n",
       "529             12       5.0        3.0   6.0   600.0               1   \n",
       "1878            12       1.0        NaN   1.0    99.0               1   \n",
       "2995            12       5.0        4.0   6.0   595.0               3   \n",
       "3272            12       4.0        3.5   7.0   599.0               1   \n",
       "3305            12       6.0        6.0   6.0  2000.0               1   \n",
       "1326            12       3.0        2.5   4.0   375.0               1   \n",
       "1656            12       4.0        3.5   6.0   283.0               4   \n",
       "1875            12       1.0        NaN   1.0    99.0               1   \n",
       "808             12       5.0        2.0   5.0   215.0               2   \n",
       "571             13       6.0        3.0   8.0   720.0               1   \n",
       "3524            13       5.0        4.5   5.0   295.0               3   \n",
       "685             14       5.0        3.0   7.0   399.0               1   \n",
       "1403            14       5.0        2.0   7.0   599.0               1   \n",
       "562             14       5.0        3.0   7.0   599.0               1   \n",
       "1658            14       4.0        3.5   7.0   283.0               4   \n",
       "657             15       6.0        3.0   9.0   500.0               1   \n",
       "2688            15       5.0        3.5  10.0   749.0               3   \n",
       "1850            15       3.0        2.0  10.0   180.0               1   \n",
       "1806            15       5.0        3.0   9.0   330.0               1   \n",
       "2106            15       6.0        4.5  12.0   499.0               2   \n",
       "2757            15       5.0        4.0   9.0   245.0               3   \n",
       "611             16       8.0        8.0  16.0  1250.0               1   \n",
       "1818            16       1.0        0.5  16.0    10.0               1   \n",
       "1402            16       8.0        6.0  13.0  1200.0               3   \n",
       "763             16       1.0        1.0   1.0  1000.0               1   \n",
       "2560            16       1.0        1.0   2.0    60.0               3   \n",
       "1594            16      10.0        8.0  13.0  1250.0               3   \n",
       "1224            16       1.0        2.0   1.0   499.0               1   \n",
       "1596            16       5.0        3.5   5.0   299.0               3   \n",
       "\n",
       "      maximum_nights  number_of_reviews  distance  predicted_price  \n",
       "2850            1125                105         2             83.6  \n",
       "2279            1125                  8         2             83.6  \n",
       "2771             730                 19         2            340.4  \n",
       "910             1125                  1         2            340.4  \n",
       "2434            1125                  9         2            340.4  \n",
       "965             1000                 74         2            340.4  \n",
       "1305            1125                 21         2             83.6  \n",
       "2513            1125                  8         2             83.6  \n",
       "2118               3                 43         2            340.4  \n",
       "345             1125                  2         2            340.4  \n",
       "725             1125                  0         2             83.6  \n",
       "1172            1125                  5         2             83.6  \n",
       "1409            1125                  2         2             83.6  \n",
       "1943            1125                  7         2             83.6  \n",
       "2842              14                  1         2            340.4  \n",
       "2967             365                 24         2            340.4  \n",
       "3295            2000                  2         2             83.6  \n",
       "3558            1125                  3         2             83.6  \n",
       "1698            1125                  2         2            340.4  \n",
       "3151              14                  0         2             83.6  \n",
       "2025            1125                195         2            340.4  \n",
       "754             1125                  2         2             83.6  \n",
       "252             1125                  0         2             83.6  \n",
       "1767              25                  0         2             83.6  \n",
       "1241             180                 12         2             83.6  \n",
       "1149            1125                 11         2             83.6  \n",
       "1931            1125                 24         2             83.6  \n",
       "3535            1125                  0         2             83.6  \n",
       "3422            1125                 15         2            340.4  \n",
       "2969               3                  2         2             83.6  \n",
       "...              ...                ...       ...              ...  \n",
       "542             1125                  0         9            340.4  \n",
       "529             1125                  0         9            340.4  \n",
       "1878             365                  0         9            340.4  \n",
       "2995              30                  0         9            340.4  \n",
       "3272            1125                  0         9            340.4  \n",
       "3305            1125                  0         9            340.4  \n",
       "1326            1125                  4         9            340.4  \n",
       "1656              90                 26         9            340.4  \n",
       "1875             365                  1         9            340.4  \n",
       "808             1825                 34         9            340.4  \n",
       "571             1125                  1        10            340.4  \n",
       "3524            1125                  1        10            340.4  \n",
       "685             1125                  2        11            340.4  \n",
       "1403            1125                  0        11            340.4  \n",
       "562             1125                  0        11            340.4  \n",
       "1658              90                 19        11            340.4  \n",
       "657               28                 41        12            340.4  \n",
       "2688              30                  0        12            340.4  \n",
       "1850            1125                 26        12            340.4  \n",
       "1806            1125                  6        12            340.4  \n",
       "2106            1125                  0        12            340.4  \n",
       "2757              30                  0        12            340.4  \n",
       "611             1125                  0        13            340.4  \n",
       "1818               2                  0        13            340.4  \n",
       "1402            1125                 10        13            340.4  \n",
       "763             1125                  0        13            340.4  \n",
       "2560              60                  0        13            340.4  \n",
       "1594            1125                  0        13            340.4  \n",
       "1224            1125                  0        13            340.4  \n",
       "1596             365                  8        13            340.4  \n",
       "\n",
       "[931 rows x 10 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df['predicted_price'] = test_df.accommodates.apply(predict_price,feature_column='accommodates')\n",
    "test_df.head(10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这样我们就得到了测试集中，所以房子的价格了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "root mean squared error (RMSE)均方根误差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"8.png\" style=\"width:700px;height:100px;float:left\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "212.98927967051543"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df['squared_error'] = (test_df['predicted_price'] - test_df['price'])**(2)\n",
    "mse = test_df['squared_error'].mean()\n",
    "rmse = mse ** (1/2)\n",
    "rmse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在我们得到了对于一个变量的模型评估得分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 不同的变量效果会不会不同呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RMSE for the accommodates column: 212.98927967051543\n",
      "RMSE for the bedrooms column: 216.49048609414763\n",
      "RMSE for the bathrooms column: 216.89419042215684\n",
      "RMSE for the number_of_reviews column: 240.21528314334847\n"
     ]
    }
   ],
   "source": [
    "for feature in ['accommodates','bedrooms','bathrooms','number_of_reviews']:\n",
    "    test_df['predicted_price'] = test_df.accommodates.apply(predict_price,feature_column=feature)\n",
    "\n",
    "    test_df['squared_error'] = (test_df['predicted_price'] - test_df['price'])**(2)\n",
    "    mse = test_df['squared_error'].mean()\n",
    "    rmse = mse ** (1/2)\n",
    "    print(\"RMSE for the {} column: {}\".format(feature,rmse))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "看起来结果差异还是蛮大的，接下来我们要做的就是综合利用所有的信息来一起进行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3671, 8)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accommodates</th>\n",
       "      <th>bedrooms</th>\n",
       "      <th>bathrooms</th>\n",
       "      <th>beds</th>\n",
       "      <th>price</th>\n",
       "      <th>minimum_nights</th>\n",
       "      <th>maximum_nights</th>\n",
       "      <th>number_of_reviews</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.401420</td>\n",
       "      <td>-0.249501</td>\n",
       "      <td>-0.439211</td>\n",
       "      <td>0.297386</td>\n",
       "      <td>0.081119</td>\n",
       "      <td>-0.341421</td>\n",
       "      <td>-0.016575</td>\n",
       "      <td>-0.516779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.399466</td>\n",
       "      <td>2.129508</td>\n",
       "      <td>2.969551</td>\n",
       "      <td>1.141704</td>\n",
       "      <td>1.462622</td>\n",
       "      <td>-0.065047</td>\n",
       "      <td>-0.016606</td>\n",
       "      <td>1.706767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.095648</td>\n",
       "      <td>-0.249501</td>\n",
       "      <td>1.265170</td>\n",
       "      <td>-0.546933</td>\n",
       "      <td>-0.718699</td>\n",
       "      <td>-0.065047</td>\n",
       "      <td>-0.016575</td>\n",
       "      <td>-0.482571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.596625</td>\n",
       "      <td>-0.249501</td>\n",
       "      <td>-0.439211</td>\n",
       "      <td>-0.546933</td>\n",
       "      <td>-0.391501</td>\n",
       "      <td>-0.341421</td>\n",
       "      <td>-0.016575</td>\n",
       "      <td>-0.516779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.401420</td>\n",
       "      <td>-0.249501</td>\n",
       "      <td>-0.439211</td>\n",
       "      <td>-0.546933</td>\n",
       "      <td>-0.718699</td>\n",
       "      <td>1.316824</td>\n",
       "      <td>-0.016575</td>\n",
       "      <td>-0.516779</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   accommodates  bedrooms  bathrooms      beds     price  minimum_nights  \\\n",
       "0      0.401420 -0.249501  -0.439211  0.297386  0.081119       -0.341421   \n",
       "1      1.399466  2.129508   2.969551  1.141704  1.462622       -0.065047   \n",
       "2     -1.095648 -0.249501   1.265170 -0.546933 -0.718699       -0.065047   \n",
       "3     -0.596625 -0.249501  -0.439211 -0.546933 -0.391501       -0.341421   \n",
       "4      0.401420 -0.249501  -0.439211 -0.546933 -0.718699        1.316824   \n",
       "\n",
       "   maximum_nights  number_of_reviews  \n",
       "0       -0.016575          -0.516779  \n",
       "1       -0.016606           1.706767  \n",
       "2       -0.016575          -0.482571  \n",
       "3       -0.016575          -0.516779  \n",
       "4       -0.016575          -0.516779  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "features = ['accommodates','bedrooms','bathrooms','beds','price','minimum_nights','maximum_nights','number_of_reviews']\n",
    "\n",
    "dc_listings = pd.read_csv('listings.csv')\n",
    "\n",
    "dc_listings = dc_listings[features]\n",
    "\n",
    "dc_listings['price'] = dc_listings.price.str.replace(\"\\$|,\",'').astype(float)\n",
    "\n",
    "dc_listings = dc_listings.dropna()\n",
    "\n",
    "dc_listings[features] = StandardScaler().fit_transform(dc_listings[features])\n",
    "\n",
    "normalized_listings = dc_listings\n",
    "\n",
    "print(dc_listings.shape)\n",
    "\n",
    "normalized_listings.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "norm_train_df = normalized_listings.copy().iloc[0:2792]\n",
    "norm_test_df = normalized_listings.copy().iloc[2792:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多变量距离的计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"9.png\" style=\"width:700px;height:400px;float:left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "scipy中已经有现成的距离的计算工具了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.723019604017032"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.spatial import distance\n",
    "\n",
    "first_listing = normalized_listings.iloc[0][['accommodates', 'bathrooms']]\n",
    "fifth_listing = normalized_listings.iloc[20][['accommodates', 'bathrooms']]\n",
    "first_fifth_distance = distance.euclidean(first_listing, fifth_listing)\n",
    "first_fifth_distance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多变量KNN模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7894063922577537\n"
     ]
    }
   ],
   "source": [
    "def predict_price_multivariate(new_listing_value,feature_columns):\n",
    "    temp_df = norm_train_df\n",
    "    temp_df['distance'] = distance.cdist(temp_df[feature_columns],[new_listing_value[feature_columns]])\n",
    "    temp_df = temp_df.sort_values('distance')\n",
    "    knn_5 = temp_df.price.iloc[:5]\n",
    "    predicted_price = knn_5.mean()\n",
    "    return(predicted_price)\n",
    "\n",
    "cols = ['accommodates', 'bathrooms']\n",
    "norm_test_df['predicted_price'] = norm_test_df[cols].apply(predict_price_multivariate,feature_columns=cols,axis=1)    \n",
    "norm_test_df['squared_error'] = (norm_test_df['predicted_price'] - norm_test_df['price'])**(2)\n",
    "mse = norm_test_df['squared_error'].mean()\n",
    "rmse = mse ** (1/2)\n",
    "print(rmse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用Sklearn来完成KNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "cols = ['accommodates','bedrooms']\n",
    "knn = KNeighborsRegressor()\n",
    "knn.fit(norm_train_df[cols], norm_train_df['price'])\n",
    "two_features_predictions = knn.predict(norm_test_df[cols])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.842682470482\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "two_features_mse = mean_squared_error(norm_test_df['price'], two_features_predictions)\n",
    "two_features_rmse = two_features_mse ** (1/2)\n",
    "print(two_features_rmse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "加入更多的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.82438385308802853"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn = KNeighborsRegressor()\n",
    "\n",
    "cols = ['accommodates','bedrooms','bathrooms','beds','minimum_nights','maximum_nights','number_of_reviews']\n",
    "\n",
    "knn.fit(norm_train_df[cols], norm_train_df['price'])\n",
    "four_features_predictions = knn.predict(norm_test_df[cols])\n",
    "four_features_mse = mean_squared_error(norm_test_df['price'], four_features_predictions)\n",
    "four_features_rmse = four_features_mse ** (1/2)\n",
    "four_features_rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
